//
//  TTButton.swift
//  fffff
//
//  Created by Taurin on 4/22/20.
//  Copyright © 2020 TTProduct. All rights reserved.
//

import UIKit

open class TTButton: UIButton {
    
    public var spacing: CGFloat = 0
    public var offset: CGPoint = .zero { willSet {
        titleOffset = newValue
        imageOffset = newValue
    } }
    
    public var titleOffset: CGPoint = .zero
    public var imageOffset: CGPoint = .zero
    public var titleAlignment: TTTitleAlignment = .left
    public var disableHighlighted: Bool = false
    
    public var isPan: Bool = false { didSet {
        guard isPan else {
            self.removeGestureRecognizer(pan)
            return
        }
       
        self.addGestureRecognizer(pan)
    } }
    
    public override var isHighlighted: Bool {
        set{
            if (disableHighlighted) { return }
            super.isHighlighted = newValue
        }
        get {
            return super.isHighlighted
        }
    }
    
    private lazy var pan: UIPanGestureRecognizer = {
        let pan = UIPanGestureRecognizer(target: self, action:#selector(self.didPanGuestureRecognizer(pan:)))
        return pan
    }()
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        titleLabel?.sizeToFit()
        
        // self
        let selfWidth = frame.width
        let selfHeight = frame.height
        
        // imageView
        let imageViewWidth = imageView?.frame.width ?? 0
        let imageViewHeight = imageView?.frame.height ?? 0
        
        // titleLabel
        let titleLabelWidth = titleLabel?.frame.width ?? 0
        let titleLabelHeight = titleLabel?.frame.height ?? 0
        
        // 水平 总宽度
        let totalWidth = imageViewWidth + spacing + titleLabelWidth
        // 水平 左间距
        let horizontalLeftMargin = (selfWidth - totalWidth) * 0.5
        
        // 垂直 总高度
        let totalHeight = imageViewHeight + spacing + titleLabelHeight
        // 垂直 上间距
        let verticalTopMargin = (selfHeight - totalHeight) * 0.5
        
        // titleLabel X位置
        var titleLabelX: CGFloat = 0
        var titleLabelY: CGFloat = 0
        // titleLabel Y位置
        var imageViewX: CGFloat = 0
        var imageViewY: CGFloat = 0
        
        // titleLabel left
        if (titleAlignment == .left) {
            titleLabelY = (selfHeight - titleLabelHeight) * 0.5
            titleLabelX = horizontalLeftMargin
            
            imageViewX = horizontalLeftMargin + titleLabelWidth + spacing
            imageViewY = (selfHeight - imageViewHeight) * 0.5
        }
        // titleLabel right
        if (titleAlignment == .right) {
            imageViewX = horizontalLeftMargin
            imageViewY = (selfHeight - imageViewHeight) * 0.5
            
            titleLabelY = (selfHeight - titleLabelHeight) * 0.5
            titleLabelX = horizontalLeftMargin + imageViewWidth + spacing
        }
        // titleLabel top
        if (titleAlignment == .top) {
            titleLabelY = verticalTopMargin
            titleLabelX = (selfWidth - titleLabelWidth) * 0.5
            
            imageViewY = verticalTopMargin + titleLabelHeight + spacing
            imageViewX = (selfWidth - imageViewWidth) * 0.5
        }
        // titleLabel bottom
        if (titleAlignment == .bottom) {
            imageViewX = (selfWidth - imageViewWidth) * 0.5
            imageViewY = verticalTopMargin
            
            titleLabelX = (selfWidth - titleLabelWidth) * 0.5
            titleLabelY = verticalTopMargin + imageViewHeight + spacing
        }
        
        titleLabelX = titleLabelX + titleOffset.x
        titleLabelY = titleLabelY + titleOffset.y
        
        imageViewX  = imageViewX  + imageOffset.x
        imageViewY  = imageViewY  + imageOffset.y
        
        // titleLabel Frame
        titleLabel?.frame = CGRect(x: titleLabelX, y: titleLabelY, width: titleLabelWidth, height: titleLabelHeight)
        
        // imageView Frame
        imageView?.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewWidth, height: imageViewHeight)
    }
}

extension TTButton {
    
    @objc private func didPanGuestureRecognizer(pan:UIPanGestureRecognizer) {
        guard let view = pan.view else { return }
        
        let translation = pan.translation(in: view.superview)
        view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
        pan.setTranslation(.zero, in: view.superview)
    }
}
